<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  talk_model.php
//  phpweb
//  
//  Created by WillLee on 2011-07-11.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class Talk_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->load->model('user_model');
			$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
			$this->uid = $this->uri->segment(3) ? intval($this->uri->segment(3)) : $this->user['uid'];
		}
		// 得到uid 我说
		function get_talks($uid, $num = '50', $offset = ''){
			$data = $talks =  array();
			if ($uid) {
				$offset = $offset ? $offset : 0;
				$this->db->where('uid', $uid);
				$data['count'] = $this->db->count_all_results('talk');
				if ($data['count']) {
					$this->db->where('uid', $uid);
					$this->db->order_by('dateline', 'DESC');
					$query = $this->db->get('talk', $num, $offset);
					foreach ($query->result_array() as $value) {
						$talks[] = $value;
					}
				}
				$data['list'] = $talks;
				if ($data) {
					return $data;
				}else{
					return false;
				}
			}
			return false;
		}
		
		function add($data){
			if ($data) {
				$this->db->insert('talk', $data);
				return $this->db->insert_id();
			}
			return false;
		}
		
		//我说详情
		function get_talk($key, $value){
			if ($key && $value) {
				$this->db->where($key, $value);
				$query = $this->db->get('talk');
				$data = $query->row_array();
				return $data;
			}
			return false;
		}
		
		// 得到分页数据的mytalk
		function get_pagetalks($uid){
			if ($uid) {
				//page
				$_SGLOBAL = $this->config->item('SGLOBAL');
				$_SGLOBAL['multipage_config']['base_url'] = base_url().'talk/index/'.$this->uid;
				$_SGLOBAL['multipage_config']['uri_segment'] = '4';
				// $_SGLOBAL['multipage_config']['per_page'] = '2';//debug
				$offset = $this->uri->segment($_SGLOBAL['multipage_config']['uri_segment']);
				$data = $this->get_talks($uid, $_SGLOBAL['multipage_config']['per_page'], $offset);
				if ($data) {
					//page
					$_SGLOBAL['multipage_config']['total_rows'] = $data['count'];
					$this->pagination->initialize($_SGLOBAL['multipage_config']);
					$multi = $this->pagination->create_links();
					$data['multi'] = $multi;
					return $data;
				}else{
					return array();
				}
			}
			return false;
		}
		
		//用户删除我说
		function remove($talkid){
			if ($talkid) {
				// 删除评论
				$this->db->where('talkid', $talkid);
				$this->db->delete('talk');
				// 删除评论
				$this->db->where('id', $talkid);
				$this->db->where('idtype', 'talkid');
				$this->db->delete('comment');
				return true;
			}
			return false;
		}
}
?>